<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'home';
$return=array();
$postdata=get_postdata();
$openid=$_SESSION['openid'];
$total=pdo_fetchcolumn("SELECT * FROM ".tablename('jy_kefu_chat')." WHERE setopenid = '{$openid}'  OR  getopenid = '{$openid}' AND uniacid = '{$_SESSION['uniacid']}' AND isdelfans = 0");
$pindex = max(1, intval($postdata['page']));
$psize = 10;
$totalpage=ceil($total/$psize) + 1;
$totalpage=intval($totalpage);
$limit = $total-$pindex*$psize;
if($limit < 0){
	$limit = 0;
}
$chatlog = pdo_fetchall("SELECT * FROM ".tablename('jy_kefu_chat')." WHERE setopenid = '{$openid}'  OR  getopenid = '{$openid}' AND uniacid = '{$_SESSION['uniacid']}' AND isdelfans = 0 ORDER BY createtime ASC LIMIT ".$limit.",".$psize);
$timestamp = TIMESTAMP;
$chatlogtime = 0;
if($chatlog){
	foreach($chatlog as $k=>$v){
				if(($v['createtime'] - $chatlogtime) > 7200){
					$chatlog[$k]['createtime'] = date('20y-m-d H:i:s',$v['createtime']);
				}else{
					$chatlog[$k]['createtime'] = '';
				}
				$chatlogtime = $v['createtime'];
				$chatlog[$k]['msg'] = preg_replace('/\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]/', '[无法识别字符]', $v['msg']);
				//$chatlog[$k]['msg'] = cfc('tools')->filterword($chatlog[$k]['msg'],$keywords);
				$regex = '@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))@';
				preg_match_all($regex,$chatlog[$k]['msg'],$array2);  
				if(!empty($array2[0]) && ($v['type'] == 1 || $v['type'] == 2)){
					foreach($array2[0] as $kk=>$vv){
						if(!empty($vv)){
							$chatlog[$k]['msg'] = str_replace($vv,"<a href='".$vv."'>".$vv."</a>",$chatlog[$k]['msg']);
						}
					}
				}
				if($v['type'] == 5 || $v['type'] == 6){
					$donetime = $timestamp - $v['createtime'];
					if($donetime >= 24*3600*3){
						unset($chatlog[$k]);
					}
				}
	}
	$html = '';
	foreach($chatlog as $k=>$v){
		$html .= !empty($v['time']) ? '<div class="logtime">'.date('Y-m-d H:i:s',$v['time']).'</div>' : '';
		$html .= $v['setopenid'] == $openid ? '<div class="r clearfix"><img class="pic"/><p class="text">'.$v['msg'].'</p></div>' : '';
		$html .= $v['setopenid'] != $openid ? '<div class="l clearfix"><img class="pic"/><p class="text">'.$v['msg'].'</p></div>' : '';
	}
}
$return['html']=$html;
exi($return);